<template>
	<xn-form-container
		:title="formData.id ? '编辑班次管理' : '增加班次管理'"
		:width="700"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="班次名称：" name="shiftName">
				<a-input v-model:value="formData.shiftName" placeholder="请输入班次名称" allow-clear />
			</a-form-item>
			<a-form-item label="上班时间：" name="upTime">
				<a-time-picker
					format="HH:mm"
					v-model:value="upTime"
					label="上班时间"
					placeholder="请选择上班时间"
					style="width: 100%"
					@change="onChange"
				>
				</a-time-picker>
			</a-form-item>
			<a-form-item label="下班时间：" name="downTime">
				<a-time-picker
					format="HH:mm"
					v-model:value="downTime"
					label="下班时间"
					placeholder="请选择下班时间"
					style="width: 100%"
					@change="onChange1"
				>
				</a-time-picker>
			</a-form-item>
			<a-form-item label="休息时间：" name="restStartTime">
				<a-time-picker
					format="HH:mm"
					v-model:value="restStartTime"
					label="休息时间"
					placeholder="请选择休息时间"
					style="width: 100%"
					@change="onChange2"
				>
				</a-time-picker>
			</a-form-item>
			<a-form-item label="休息时间：" name="restEndTime">
				<a-time-picker
					format="HH:mm"
					v-model:value="restEndTime"
					label="休息时间"
					placeholder="请选择休息时间"
					style="width: 100%"
					@change="onChange3"
				>
				</a-time-picker>
			</a-form-item>
			<a-form-item label="是否启用：" name="status">
				<a-switch :checked="isRestFlag" @change="editStatus" />
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="sysShiftManageForm">
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import sysShiftManageApi from '@/api/biz/sysShiftManageApi'
	import moment from 'moment'
	// 抽屉状态
	const visible = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)
	const upTime = ref({})
	const downTime = ref({})
	const restStartTime = ref({})
	const restEndTime = ref({})
	const isRestFlag = ref(false)

	// 打开抽屉
	const onOpen = (record) => {
		visible.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
			upTime.value = moment(formData.value.upTime, 'HH:mm')
			downTime.value = moment(formData.value.downTime, 'HH:mm')
			restStartTime.value = moment(formData.value.restStartTime, 'HH:mm')
			restEndTime.value = moment(formData.value.restEndTime, 'HH:mm')
			isRestFlag.value = formData.value.status == 1 ? true : false
		} else {
			upTime.value = moment('00:00', 'HH:mm')
			downTime.value = moment('00:00', 'HH:mm')
			restStartTime.value = moment('00:00', 'HH:mm')
			restEndTime.value = moment('00:00', 'HH:mm')
		}
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		visible.value = false
	}
	/**
	 * 日期需单独转换
	 */
	const onChange = (date, dateString) => {
		if (date == null) {
			formData.value.upTime = ''
		} else {
			formData.value.upTime = dateString
		}
	}
	const onChange1 = (date, dateString) => {
		if (date == null) {
			formData.value.downTime = ''
		} else {
			formData.value.downTime = dateString
		}
	}
	const onChange2 = (date, dateString) => {
		if (date == null) {
			formData.value.restStartTime = ''
		} else {
			formData.value.restStartTime = dateString
		}
	}
	const onChange3 = (date, dateString) => {
		if (date == null) {
			formData.value.restEndTime = ''
		} else {
			formData.value.restEndTime = dateString
		}
	}
	const editStatus = (r) => {
		isRestFlag.value = r
		formData.value.status = r
	}
	// 默认要校验的
	const formRules = {}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value.validate().then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			formDataParam.status = formDataParam.status == true ? 1 : 0
			sysShiftManageApi
				.sysShiftManageSubmitForm(formDataParam, formDataParam.id)
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
